package database;

import scripting.AbstractPlayerInteraction;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DatabaseScriptAPI {

    public static List<Map<String, Object>> SQLSelect(String sql, Object... data) {
        List<Map<String, Object>> ret = new ArrayList<>();
        try (Connection con = DatabaseConnection.getConnection()) {
            try (PreparedStatement ps = con.prepareStatement(sql)) {
                for (int i = 0; i < data.length; i++) {
                    ps.setObject(i + 1, data[i]);
                }
                try (ResultSet rs = ps.executeQuery()) {
                    ResultSetMetaData metaData = ps.getMetaData();
                    while (rs.next()) {
                        Map<String, Object> rsdata = new HashMap<>();
                        for (int i = 0; i < metaData.getColumnCount(); i++) {
                            String columnLabel = metaData.getColumnLabel(i + 1);
                            rsdata.put(columnLabel, rs.getObject(columnLabel));
                        }
                        if (!rsdata.isEmpty()) {
                            ret.add(rsdata);
                        }
                    }
                }
            }
        } catch (SQLException e) {
            AbstractPlayerInteraction.log.error("查询数据出错", e);
        }
        return ret;
    }

    public static int SQLInsert(String sql, Object... data) {
        int ret = 0;
        Connection con = DatabaseConnection.getConnection();
        try {
            con.setAutoCommit(false);
            try (PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
                for (int i = 0; i < data.length; i++) {
                    ps.setObject(i + 1, data[i]);
                }
                ret = ps.executeUpdate();
            }
            con.commit();
        } catch (SQLException e) {
            try {
                con.rollback();
            } catch (SQLException ignored) {
            }
            AbstractPlayerInteraction.log.error("插入数据出错", e);
        } finally {
            try {
                con.setAutoCommit(true);
                con.close();
            } catch (SQLException ignored) {
            }
        }
        return ret;
    }

    public static int SQLUpdate(String sql, Object... data) {
        int ret = 0;
        Connection con = DatabaseConnection.getConnection();
        try {
            con.setAutoCommit(false);
            try (PreparedStatement ps = con.prepareStatement(sql)) {
                for (int i = 0; i < data.length; i++) {
                    ps.setObject(i + 1, data[i]);
                }
                ret = ps.executeUpdate();
            }
            con.commit();
        } catch (SQLException e) {
            try {
                con.rollback();
            } catch (SQLException ignored) {
            }
            AbstractPlayerInteraction.log.error("更新数据出错", e);
        } finally {
            try {
                con.setAutoCommit(true);
                con.close();
            } catch (SQLException ignored) {
            }
        }
        return ret;
    }
}
